﻿/*
	Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "ProgressBar.h"
#include "../render/Camera.h"
#include "../base/EventResize.h"
#include "../base/EventWidgetMove.h"
#include "../base/Debuger.h"

namespace SkyEngine2d
{
	
	ProgressBar::ProgressBar(Widget* parent /*= nullptr*/)
		:Widget(parent)
		, m_maxValue(100)
		, m_minValue(0)
		, m_currentValue(0)
	{
		this->setFocusPolicy(Widget::FocusPolicy::NoFocus);
	}
	
	void ProgressBar::draw(IRender* render, const Matrix4X4& transform)
	{
		DirectX::XMMATRIX mvp = DirectX::XMMatrixMultiply(transform, getCamera()->getViewProjectMat());
	
		if (m_currentBgImage)
		{
			m_currentBgImage->draw(render, mvp);
		}
	
		if (m_fillImag)
		{
			if (m_progressRect.size.width > 0)
			{
				m_fillImag->draw(render, m_progressRect, getCamera());
			}
		}
	}
	
	void ProgressBar::updateFillRect()
	{
		//计算 填充进度的矩形区域 
		auto rect = this->getGeometry();
		Vector2 lb = getAbsPos() + Vector2(m_paddingLeft, m_paddingBotton);
		Vector2 rt = Vector2(getAbsPos().x + rect.size.width, getAbsPos().y + rect.size.height) - Vector2(m_paddingRight, m_paddingTop);
		m_fillRect = Rect(lb, Size(rt.x - lb.x, rt.y - lb.y));
		m_progressRect = m_fillRect;
	
		//更新当前填充的区域
		this->updateProgress();
	
	}
	
	bool ProgressBar::init()
	{
		if (!Widget::init())
		{
			return false;
		}
	
		return true;
	}
	
	
	void ProgressBar::onMoveEvent(EventWidgetMove * e)
	{
		Widget::onMoveEvent(e);
	
		updateFillRect();
	}
	
	void ProgressBar::onResizeEvent(EventResize * e)
	{
		Widget::onResizeEvent(e);
		updateFillRect();
	}
	
	void ProgressBar::updateProgress()
	{
		float p = (float)(m_currentValue) / m_maxValue;
		m_progressRect.size.width = p * m_fillRect.size.width;
	}
	
	void ProgressBar::onWidgetStateChange()
	{
		this->updataStyleInfo();
	}
	
	void ProgressBar::onWidgetStyleChange()
	{
		this->updataStyleInfo();
	}
	
	void ProgressBar::updataStyleInfo()
	{
		auto p = this->getStyleInfor();
		if (p && p->backgroundImage)
		{
			m_currentBgImage = p->backgroundImage.get();
		}
		else
		{
			auto pp = this->getStyleInforByState(WidgetState::Normal);
			m_currentBgImage = pp->backgroundImage.get();
		}
	}
	
	
	ProgressBar* ProgressBar::create(Widget* parent /*=nullptr*/)
	{
		auto p = makeWidgetPtr(parent);
		if (p && p->init())
		{
			return p;
		}
		return nullptr;
	}
	
	ProgressBar::~ProgressBar()
	{
	
	}
	
	
	void ProgressBar::setFillImage(std::unique_ptr<Scale9Image> image)
	{
		if (image)
		{
			m_fillImag = std::move(image);
			m_fillImag->setDisplaySize(m_fillRect.size);
		}
	}
	
	void ProgressBar::setPadding(float top, float left, float bottom, float right)
	{
		m_paddingBotton = bottom;
		m_paddingLeft = left;
		m_paddingRight = right;
		m_paddingTop = top;
	
		this->updateFillRect();
	}
	
	void ProgressBar::setMaxValue(int max_value)
	{
		m_maxValue = max_value;
		this->updateProgress();
	}
	
	void ProgressBar::setMinValue(int min_value)
	{
		m_minValue = min_value >= 0 ? min_value : 0;
		this->updateProgress();
	}
	
	void ProgressBar::setCurrentValue(int value)
	{
		m_currentValue = value >= m_maxValue ? m_maxValue : value;
		m_currentValue = value <= m_minValue ? m_minValue : value;
	
		this->updateProgress();
	}
	
	int ProgressBar::getMaxValue() const
	{
		return m_maxValue;
	}
	
	int ProgressBar::getMinValue() const
	{
		return m_minValue;
	}
	
	int ProgressBar::getCurrentValue() const
	{
		return m_currentValue;
	}
}